import { store } from "@/store";
import { defineStore } from "pinia";
import SupplierAgentAPI, {
  type SupplierAgentListQuery,
  type SupplierAgentTransactionListQuery,
  type SupplierAgentItemType,
} from "@/api/supplier/agent";

export const useSupplierAgentStore = defineStore("supplier-agent", () => {
  const list = ref<SupplierAgentItemType[]>([]);

  const transactionList = ref<SupplierAgentItemType[]>([]);

  const totalCount = ref(0);

  const totalCount2 = ref(0);

  const totalPage = ref(0);

  const totalPage2 = ref(0);

  const loading = reactive({
    list: false,
    transactionList: false,
  });

  async function fetchList(params: SupplierAgentListQuery) {
    loading.list = true;
    const response = await SupplierAgentAPI.getSupplierAgentList(params);
    loading.list = false;
    if (response) {
      list.value = response.records;
      totalCount.value = response.total_count;
      totalPage.value = response.total_page;
    }
  }

  async function fetchTransactionList(params: SupplierAgentTransactionListQuery) {
    loading.transactionList = true;
    const response = await SupplierAgentAPI.getSupplierAgentTransactionList(params);
    loading.transactionList = false;
    if (response) {
      transactionList.value = response.records;
      totalCount2.value = response.total_count;
      totalPage2.value = response.total_page;
    }
  }

  return {
    list,
    transactionList,
    totalCount,
    totalPage,
    loading,
    fetchList,
    fetchTransactionList,
  };
});

export const useSupplierAgentStoreHook = () => {
  return useSupplierAgentStore(store);
};
